
/**********************************************************************
***********************************************************************
* FILE		: Mbcallback.c
* Description	: 
*			  
* Copyright (c) 2013 by BTCS. All Rights Reserved.
* 
* History:
* Version		Name       		Date			Description
   0.1		AlexZhang	2013/01/02	Initial Version
   
***********************************************************************
**********************************************************************/

#include "includes.h"

/* ----------------------- Defines ------------------------------------------*/
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4

/* ----------------------- Static variables ---------------------------------*/
static USHORT   usRegInputStart = REG_INPUT_START;
static USHORT   usRegInputBuf[REG_INPUT_NREGS];
/* ----------------------- Modbus modular end -------------------------------*/

#define REG_HOLDING_START 40001//actual the software
#define REG_HOLDING_NREGS 100

USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
USHORT *usM1Vol = &usRegHoldingBuf[0];//reg:0
USHORT *usM1Freq = &usRegHoldingBuf[1];//reg:1

/* ----------------------- Modbus modular start -----------------------------*/
eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;

    if( ( usAddress >= REG_INPUT_START )
        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
    {
        iRegIndex = ( int )( usAddress - usRegInputStart );
        while( usNRegs > 0 )
        {
            *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
            *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
            iRegIndex++;
            usNRegs--;
        }
    }
    else
    {
        eStatus = MB_ENOREG;
    }

    return eStatus;
}

eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
{
    eMBErrorCode    eStatus = MB_ENOERR;
    int             iRegIndex;
    USHORT i = 0;
    
    *usM1Vol = 0x1122;
    *usM1Freq = 0x3344;
    if( ( usAddress >= REG_HOLDING_START )
    && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
    {
        iRegIndex = ( int )( usAddress - REG_HOLDING_START );
        if(eMode == MB_REG_READ)
            {
            while(usNRegs--)
                {
                *pucRegBuffer++ = (UCHAR)( usRegHoldingBuf[iRegIndex] >> 8 );
                *pucRegBuffer++ = (UCHAR)( usRegHoldingBuf[iRegIndex] & 0xFF );
                iRegIndex++;
                }
        eStatus =  MB_ENOERR;
        }
    }else{
        eStatus =  MB_ENOREG;
    }
    
    return eStatus;
}


eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
    ( void )pucRegBuffer;
    ( void )usAddress;
    ( void )usNCoils;
    ( void )eMode;
    return MB_ENOREG;
}

eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
    ( void )pucRegBuffer;
    ( void )usAddress;
    ( void )usNDiscrete;
    return MB_ENOREG;
}
/* ----------------------- Modbus modular end -------------------------------*/

